package io.renren.modules.sys.controller;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.entity.DoctorEntity;
import io.renren.modules.sys.service.DoctorService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 医生服务表
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2019-11-11 17:27:53
 */
@RestController
@RequestMapping("sys/doctor")
public class DoctorController {
    @Autowired
    private DoctorService doctorService;

    @Autowired
    private SysUserService sysUserService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:doctor:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = doctorService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:doctor:info")
    public R info(@PathVariable("id") Integer id){
        DoctorEntity doctor = doctorService.getById(id);

        return R.ok().put("doctor", doctor);
    }

    /**
     * 信息
     */
    @RequestMapping("/infobyuserid/{id}")
    @RequiresPermissions("sys:doctor:infobyuserid")
    public R infobyuserid(@PathVariable("id") Integer id){
        DoctorEntity doctor = doctorService.getDoctorByUserid(id);

        return R.ok().put("doctor", doctor);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:doctor:save")
    public R save(@RequestBody DoctorEntity doctor){
        doctorService.save(doctor);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:doctor:update")
    public R update(@RequestBody DoctorEntity doctor){
        ValidatorUtils.validateEntity(doctor);
        doctorService.updateById(doctor);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{id}")
    @RequiresPermissions("sys:doctor:delete")
    public R delete(@PathVariable("id") Integer id){
        DoctorEntity entity =  doctorService.getById(id);
        if(entity !=null){
            doctorService.removeById(id);
            sysUserService.removeById(entity.getUserid());
        }

        return R.ok();
    }

    /**
     * 医生列表
     */
    @RequestMapping("/doctorlist")
    @RequiresPermissions("sys:doctor:doctorlist")
    public R doctorlist(){
        List<DoctorEntity> list = doctorService.list(new QueryWrapper<DoctorEntity>());

        return R.ok().put("list", list);
    }
}
